package com.william.xiaobujiantest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import static android.content.ContentValues.TAG;
import static com.william.xiaobujiantest.Whats.RESTARTSERVICE;

/**
 * 系统常驻服务，带有重启机制，避免被kill
 */
public class MainService extends Service {

    public MainService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        return super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        //销毁后发起一个广播，由widget接收，并重启service
        //静态注册,实测动态注册不好用，因为不好取消注册
        Intent localIntent = new Intent();
        localIntent.setAction(RESTARTSERVICE);
        sendBroadcast(localIntent);
        Log.i(TAG, "onDestroy: 服务已退出，广播已发送！"+RESTARTSERVICE);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
